<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文化馆</title>
    <link rel="stylesheet" href="../css/ext/bootstrap.min.css">
    <link rel="stylesheet" href="../css/common/base.css">
    <script src="../js/ext/jquery-1.11.3.js"></script>
    <script src="../js/ext/bootstrap.min.js"></script>
    <script src="../js/ext/vue.js"></script>
</head>
<body>
<% include ../common/header.jsp %>
<div class="main">
    <% include ../common/menu.jsp %>
    <div class="main-content">
        <div class="wrapper">
            <!--title-->
            <div class="content-title">
                <!--面包屑-->
                <ol class="breadcrumb">
                    <li class="active">ajaxdemo</li>
                </ol>
            </div>
            <form method="post" action="/ajaxdemo/doForm">
                <input type="checkbox" name="c" value="1">
                <input type="checkbox" name="c" value="2">
                <input type="checkbox" name="c" value="3">
                <input type="checkbox" name="c" value="4">
                <input type="submit" value="提交">
            </form>
            <!--内容-->
            <select name="">
                <option value="1">1</option>
                <option value="2" selected=true>2</option>
                <option value="3">3</option>
            </select>
            <input id="firsel" type="text" value="3">
            <input id="secsel" type="text" value="2">
            <div id="test">
                <select id="sel1" v-model="selected">
                    <option value="0" selected>公有</option>
                    <option v-for="item in fir" :value="item.firid" :selected="item.firsel">
                        {{item.firname}}
                    </option>
                </select>
                <select id="sel2">
                    <option value="0" selected>公有</option>
                    <option v-for="item in sec" :value="item.secid" :selected="item.secsel">
                        {{item.secname}}
                    </option>
                </select>
            </div>
            <input type="button" onclick="doAjax()" value="ajax">
            <script>
                function doAjax() {
                    var group_list = { a:[
                    {
                        firname: '分组一',
                        firid: '1',
                        firsel: false,
                        sec: [
                            {secname: '分组1.1', secid: '1'}
                        ]
                    },
                    {
                        firname: '分组二',
                                firid: '2',
                            firsel: false,
                            sec: [
                        {secname: '分组2.1', secid: '1'},
                        {secname: '分组2.2', secid: '2'}
                    ]
                    },
                    {
                        firname: '分组三',
                                firid: '3',
                            firsel: false,
                            sec: [
                        {secname: '分组3.1', secid: '1'},
                        {secname: '分组3.2', secid: '2'},
                        {secname: '分组3.3', secid: '3'}
                    ]
                    },
                    {
                        firname: '分组四',
                                firid: '4',
                            firsel: false,
                            sec: [
                        {secname: '分组4.1', secid: '1'},
                        {secname: '分组4.2', secid: '2'},
                        {secname: '分组4.3', secid: '3'},
                        {secname: '分组4.4', secid: '4'}
                    ]
                    },
                    {
                        firname: '分组五',
                                firid: '5',
                            firsel: false,
                            sec: []
                    }
                ]};
                    $.ajax({
                        url: '../ajaxdemo/doAjaxPost',
                        type: 'post',
                        data: {
                            firselid: $('#firsel').val(),
                            secselid: $('#secsel').val(),
                            group_list: group_list
                        },
                        success: function (res) {
                            console.log(res)
                        },
                        error: function () {
                            console.log('ajax错误')
                        }
                    })
                }
                $(function () {
                    var group_list = [
                        {
                            firname: '分组一',
                            firid: '1',
                            sec: [
                                {secname: '分组1.1', secid: '1'}
                            ]
                        },
                        {
                            firname: '分组二',
                            firid: '2',
                            sec: [
                                {secname: '分组2.1', secid: '1'},
                                {secname: '分组2.2', secid: '2'}
                            ]
                        },
                        {
                            firname: '分组三',
                            firid: '3',
                            sec: [
                                {secname: '分组3.1', secid: '1'},
                                {secname: '分组3.2', secid: '2'},
                                {secname: '分组3.3', secid: '3'}
                            ]
                        },
                        {
                            firname: '分组四',
                            firid: '4',
                            sec: [
                                {secname: '分组4.1', secid: '1'},
                                {secname: '分组4.2', secid: '2'},
                                {secname: '分组4.3', secid: '3'},
                                {secname: '分组4.4', secid: '4'}
                            ]
                        },
                        {
                            firname: '分组五',
                            firid: '5',
                            sec: []
                        }
                    ];
                    var listVue = new Vue({
                        el: '#test',
                        data: {
                            selected: '全部',
                            fir: ''
                        },
                        ready: function () {
                            var _self = this;
                            $.ajax({
                                url: '../ajaxdemo/doAjax',
                                type: 'get',
                                data: {
                                    firselid: $('#firsel').val(),
                                    secselid: $('#secsel').val(),
                                    group_list: group_list
                                },
                                dataType: 'json',
                                success: function (res) {
                                    var data = res.data;
                                    var f = res.firselid;
                                    var s = res.secselid;
                                    console.log(typeof(s));
                                    console.log(typeof(group_list[0].firsel));
                                    console.log(typeof(data[0].firsel));
                                    $.each(data, function (k, v) {
                                        if (v.firid == f) {
                                            v.firsel = true;
                                            $.each(v.sec, function (k1, v1) {
                                                if (v1.secid == s) {
                                                    v1.secsel = true;
                                                }
                                            })
                                        } else {
                                            v.firsel = false;
                                        }
                                    });
                                    console.log(data);
                                    _self.fir = data;
                                }
                            });
                        },
                        computed: {
                            sec: {
                                get: function () {
                                    var that = this;//vue对象
                                    var len = this.fir.length;
                                    var arr = [];
                                    for (var i = 0; i < len; i++) {
                                        if (this.fir[i].firid == that.selected) {
                                            arr = this.fir[i].sec
                                        }
                                    }
                                    return arr;
                                }
                            }
                        }
                    })
                });

                var str = 'false';
                if (str == 'false') {
                    str = false
                }
                console.log(typeof(str))
            </script>
        </div>
    </div>
</div>
</body>
</html>